﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BookReader
{
    class Service
    {
        const int column = 20;
        public static void OpenBook(string Path, List<string> list)
        {
            list.Clear();
            int col = 0;
            StaticData.openBookString = File.ReadAllText(Path, StaticData.encoding);
            StringBuilder buffer = new StringBuilder();
            for (int p = 0; p < StaticData.openBookString.Length; p++)
            {
                if (StaticData.openBookString[p] == '\n')
                {
                    list.Add(buffer.ToString());
                    buffer.Length = 0;
                    col = 0;
                    continue;
                }
                if (col < column)
                {
                    buffer.Append(StaticData.openBookString[p]);
                }
                else
                {
                    list.Add(buffer.ToString());
                    buffer.Length = 0;
                    col = 0;
                    buffer.Append(StaticData.openBookString[p]);
                }
                if (p == (StaticData.openBookString.Length - 1))
                {
                    list.Add(buffer.ToString());
                }
                col++;
            }

        }
        public static void Save()
        {
            string configPath = AppDomain.CurrentDomain.BaseDirectory + "config.xml";
            StaticData.config.ConfigData[0].workingDir = StaticData.workingDir;
            StaticData.config.ConfigData[0].pageSize = StaticData.pageSize;
            StaticData.config.ConfigData[0].currentIndex = StaticData.currentIndex;
            StaticData.config.ConfigData[0].openFilepath = StaticData.openFilepath;
            StaticData.config.ConfigData[0].CodePage = StaticData.encoding.CodePage;
            StaticData.config.ConfigData[0].pageIndex = StaticData.PageIndex;
            StaticData.config.WriteXml(configPath, System.Data.XmlWriteMode.WriteSchema);
        }
    }
}
